﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Unity.Mathematics;
using System.Runtime.CompilerServices;

namespace Assets.fpmath
{
    internal class fpmath
    {
        //定点和浮点的转换，非极限的计算，转回浮点算其实还好
        //放大值就用 0x10000 =65536，也就是用16bit 作为固定小数部分
        //这就导致了 加法是可以用的，但是乘除法不能直接做，可能要换成浮点做了，再换回来。

        [MethodImpl(MethodImplOptions.AggressiveInlining)]
        public int tofix(float v)
        {
            return (int)(v * 0x10000);
        }

        [MethodImpl(MethodImplOptions.AggressiveInlining)]
        public float tofloat(int v)
        {
            return (float)(v >> 16) + ((float)(v & 0xffff)) / 0x10000;
        }

        [MethodImpl(MethodImplOptions.AggressiveInlining)]
        public int2 tofix(float2 v)
        {
            return new int2(tofix(v.x), tofix(v.y));
        }

        [MethodImpl(MethodImplOptions.AggressiveInlining)]
        public float2 tofloat(int2 v)
        {
            return new float2(tofloat(v.x), tofloat(v.y));
        }

        [MethodImpl(MethodImplOptions.AggressiveInlining)]
        public int3 tofix(float3 v)
        {
            return new int3(tofix(v.x), tofix(v.y), tofix(v.z));
        }

        [MethodImpl(MethodImplOptions.AggressiveInlining)]
        public float3 tofloat(int3 v)
        {
            return new float3(tofloat(v.x), tofloat(v.y), tofloat(v.z));
        }
    }
}
